package com.zk.tools.testcase;

import com.alibaba.fastjson.JSONObject;


import com.sun.mail.util.MailSSLSocketFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import javax.mail.*;
import java.io.*;
import java.util.Properties;

public class TestCase {

    private static final Logger logger = LoggerFactory.getLogger(TestCase.class);


    public static void test3() {
//        Properties props = new Properties();
//        props.setProperty("mail.popStore.protocol", "pop3");       // 使用pop3协议
//        props.setProperty("mail.pop3.port", "995");           // 端口
//
//        MailSSLSocketFactory sf = new MailSSLSocketFactory();
//        sf.setTrustAllHosts(true);
//        props.put("mail.pop3.ssl.enable",true);
//        props.put("mail.pop3.ssl.socketFactory",sf);
//
////props.setProperty("mail.debug", "true");
//        props.setProperty("mail.pop3.host", "pop.qq.com");
//
//        Session session = Session.getInstance(props);
//        pop3Store = (POP3Store) session.getStore("pop3");
//
//        pop3Store.connect("pop.qq.com", 995, user, password);
//        pop3Folder = (POP3Folder) pop3Store.getFolder("INBOX");
//        /* Folder.READ_ONLY：只读权限
//         * Folder.READ_WRITE：可读可写（可以修改邮件的状态）
//         */
//        pop3Folder.open(Folder.READ_WRITE); //打开收件箱
//
//        private void init() throws MessagingException{
//            Properties properties=new Properties();
//            //设置发送和接收协议
//            properties.put("mail.transport.protocal", "smtp");
//            properties.put("mail.store.protocal", "imap");
//            //设置ssl的端口
//            properties.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
//            properties.setProperty("mail.imap.socketFactory.fallback", "false");
//            properties.setProperty("mail.imap.port", "993");
//            properties.setProperty("mail.imap.socketFactory.port", "993");/*
//		properties.put("mail.imap.port", "993");
//		properties.put("mail.smtp.port", "465");*/
//            //smtp认证
//            properties.put("mail.smtp.auth", "true");
//            //设置发送和接收处理类
//            properties.put("mail.transport.class", "com.sun.mail.smtp.SMTPTransport");
//            properties.put("mail.imap.class", "com.sun.mail.imap.IMAPStore");
//            //设置发送邮件服务器
//            properties.put("mail.smtp.host",sendHost);
//            //获取邮件通信会话
//            Authenticator auth=new MailAuthenticator();
//            session=Session.getDefaultInstance(properties,auth);
//            session.setDebug(true);
//            //获取接收邮件对象
//            store=session.getStore("imap");
//            //连接接收邮件服务器
//            store.connect(receiveHost, null, null);
//        }
    }

    public static void test() {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        try {
            Properties props = new Properties();
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            props.put("mail.pop3.ssl.enable", true);
            props.put("mail.pop3.ssl.socketFactory", sf);
            logger.info("123");
            props.setProperty("mail.popStore.protocol", "pop3");
            props.setProperty("mail.pop3.port", "995");
            logger.info("123");
            props.setProperty("defaultEncoding", "UTF-8");
            sender.setJavaMailProperties(props);
            Session session = sender.getSession();
            Store store = session.getStore("pop3");
            store.connect("pop.qq.com", 995, "1798864457@qq.com", "**********");

            Folder folder = store.getFolder("INBOX");
            folder.open(Folder.READ_ONLY);
            Message[] msgs = folder.getMessages();

            for (Message msg : msgs) {
                try {
                    logger.info(msg.getSubject());
                } catch (Exception e) {
                    logger.error(e.getMessage(), e);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void test2() throws Exception {
        String jsonStr = "";
        Reader reader = new InputStreamReader(new FileInputStream(new File("D:/WX-WKSP/EmailUtil/emailConf.json")));
        StringBuffer sb = new StringBuffer();
        int ch = 0;
        while ((ch = reader.read()) != -1) {
            sb.append((char) ch);
        }
        jsonStr = sb.toString();
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        logger.info(jsonObject.toJSONString());

    }


    public static void main(String[] args) throws Exception {
        test();
    }

}
